package mysql

import (
	"gorm.io/gorm"
	"video/videorpc/videorpc"
)

type Comment struct {
	gorm.Model
	UserId  int    `gorm:"type:int;not null;comment:'用户id'"`
	VideoId int    `gorm:"type:int;not null;comment:'视频id'"`
	Content string `gorm:"type:varchar(500);not null;comment:'评论内容'"`
	Status  int    `gorm:"type:tinyint;not null;comment:'评论状态'"`
	ReplyId int    `gorm:"type:int;not null;comment:'回复id'"`
}

func (c *Comment) CommentAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *Comment) FindCommentById(db *gorm.DB, id int64) error {
	return db.Debug().Where("id = ?", id).Delete(c).Error
}

func CommentList(db *gorm.DB, in *videorpc.CommentListRequest) []*videorpc.CommentList {
	var list []*videorpc.CommentList

	db.Debug().Model(&Comment{}).
		Select("users.head_image, users.nick_name, comments.created_at, comments.content").
		Joins("left join users on users.id = comments.user_id").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Order("comments.created_at desc").
		Find(&list)

	return list
}
